package com.learn.demo.netty.tcp.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 消息编码
 *
 * @author banjiawei
 * @date 2022/01/15
 */
public class MessageEncoder extends MessageToByteEncoder<Message> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Message message, ByteBuf byteBuf) throws Exception {
        byteBuf.writeInt(message.getLength());
        byteBuf.writeBytes(message.getContent());
        channelHandlerContext.writeAndFlush(message);
    }
}
